package com.example.composableexample.ui

import android.net.Uri
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Send
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.DateRange
import androidx.compose.material.icons.outlined.Favorite
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import com.example.composableexample.DummySpotlightData
import com.example.composableexample.Spotlight
import com.example.composableexample.VideoPlayer
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.ui.StyledPlayerView

/**
 * author: LiLiang
 * createTime: 2022/9/21-12:32
 * remark:
 **/

val horizontalPadding = 10.dp

@Preview
@Composable
fun SpotlightScreen() {
    val spotlights = DummySpotlightData.spotlight

    Box {
        val context = LocalContext.current
        val exoPlayer = remember {
            ExoPlayer.Builder(context).build().apply {
                addMediaItem(
                    MediaItem.Builder().setUri(Uri.parse(spotlights[0].video)).build()
                )
                setMediaItem(
                    MediaItem.Builder().setUri(Uri.parse(spotlights[1].video)).build()
                )
                playWhenReady = true
                prepare()
            }
        }
        AndroidView(modifier = Modifier.fillMaxSize(), factory = {
            StyledPlayerView(context).apply {
                player = exoPlayer
            }
        })
    }


//    Box(
//        Modifier
//            .clip(RoundedCornerShape(bottomEnd = 10.dp, bottomStart = 10.dp))
//            .background(color = Color.Black)
//    ) {
//        Box(modifier = Modifier.fillMaxWidth()) {
//            VideoPlayer(uri = spotlights[0].getVideoUrl())
//        }
//        LazyColumn {
//            items(spotlights) { s ->
//                Box(
//                    modifier = Modifier.fillParentMaxHeight()
//                ) {
//                    VideoPlayer(uri = s.getVideoUrl())
//                    Column(Modifier.align(Alignment.BottomStart)) {
//                        SpotlightFooter(s)
//                        Divider()
//                    }
//                }
//            }
//        }
//    }
}

@Composable
fun SpotlightFooter(spotlight: Spotlight) {
    Row(
        Modifier
            .fillMaxWidth()
            .padding(start = 18.dp, bottom = 18.dp),
        verticalAlignment = Alignment.Bottom
    ) {
        FooterUserData(
            spotlight = spotlight,
            modifier = Modifier.weight(8f)
        )

        FooterUserAction(
            modifier = Modifier.weight(2f)
        )
    }
}

@Composable
fun FooterUserAction(modifier: Modifier) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier
    ) {
        UserAction(
            icon = Icons.Outlined.Favorite,
        )
        Spacer(modifier = Modifier.height(10.dp))
        UserAction(
            icon = Icons.Default.Send,
        )
        Spacer(modifier = Modifier.height(10.dp))
        UserAction(
            icon = Icons.Default.MoreVert,
        )
    }
}

@Composable
fun UserAction(
    icon: ImageVector,
) {
    Icon(
        imageVector = icon,
        tint = Color.White,
        modifier = Modifier
            .clip(CircleShape)
            .background(color = Color.Transparent)
            .padding(10.dp)
            .size(28.dp),
        contentDescription = null
    )
}

@Composable
fun FooterUserData(spotlight: Spotlight, modifier: Modifier) {
    Column(
        modifier = modifier,
        verticalArrangement = Arrangement.Center
    ) {

        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
        ) {
            Box(
                modifier = Modifier
                    .height(28.dp)
                    .width(28.dp),
                contentAlignment = Alignment.Center
            ) {
                val painter = rememberAsyncImagePainter(
                    ImageRequest.Builder(LocalContext.current)
                        .data(data = spotlight.userImage)
                        .apply(block = fun ImageRequest.Builder.() {
                            transformations(
                                CircleCropTransformation()
                            )
                        }).build()
                )
                Image(
                    painter = painter,
                    contentDescription = null,
                )
            }
            Spacer(modifier = Modifier.width(horizontalPadding))
            Text(
                text = "@${spotlight.userName}",
                color = Color.White,
                fontWeight = FontWeight.Bold,
                style = MaterialTheme.typography.labelMedium
            )

            Spacer(modifier = Modifier.width(horizontalPadding))
            Icon(
                modifier = Modifier.size(20.dp),
                imageVector = Icons.Outlined.FavoriteBorder,
                contentDescription = "Bookmark"
            )
            Icon(
                modifier = Modifier.size(15.dp),
                imageVector = Icons.Outlined.Add,
                contentDescription = ""
            )
        }
        Spacer(modifier = Modifier.height(horizontalPadding))

        // Audio
        Row(
            modifier = Modifier
                .clip(RoundedCornerShape(10.dp))
                .background(color = Color.Transparent)
                .padding(10.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
        ) {
            Icon(
                modifier = Modifier.size(15.dp),
                imageVector = Icons.Outlined.DateRange,
                contentDescription = "Bookmark"
            )
            Spacer(modifier = Modifier.width(10.dp))
            Text(
                text = "@${spotlight.userName}'s Sound",
                color = Color.White,
                style = MaterialTheme.typography.bodySmall
            )
        }
    }
}